#include "choosescene.h"
#include<QPainter>
#include<QMenuBar>
#include"mypushbutton.h"
#include<QTimer>
#include<QLabel>
#include<QDebug>
#include"playscene.h"
chooseScene::chooseScene(QWidget *parent)
    : QMainWindow{parent}
{

    //窗口的简单设置（大小，图标，名称）
    setFixedSize(320,588);
    setWindowIcon(QIcon(":/res/Coin0001.png"));
    setWindowTitle("选关界面");

    //创建退出的菜单项
    QMenuBar *bar = new QMenuBar();
    setMenuBar(bar);
    QMenu *startMenu = bar->addMenu("开始");
    QAction *quitAction = startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮
    myPushButton *backbtn = new myPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backbtn->setParent(this);
    backbtn->move(this->width() - backbtn->width(),this->height() - backbtn->height());
    connect(backbtn,&QPushButton::clicked,this,[=](){
        QTimer::singleShot(400,this,[=](){
            //发送信号给主场景
            emit this->ChooseSceneBack();
        });

    });

    //选关按钮
    for(int i=0;i<20;i++)
    {
        myPushButton *levelbtn = new myPushButton(":/res/LevelIcon.png");
        levelbtn->setParent(this);
        levelbtn->move(i%4 *70+25, i/4*70+140);

        //监听按钮的点击
        connect(levelbtn,&myPushButton::clicked,this,[=](){
            //下一关的创建
            play_scene = new playScene(i+1);
            //设置play_scene出现位置
            play_scene->setGeometry(this->geometry());
            this->hide();
            //进入游戏场景
            play_scene->show();

            //返回选关界面
            //监听游戏关卡的返回信号 且必须写在连接按钮和关卡场景之后，因为点击之后才有scene的场景
            connect(play_scene,&playScene::PlaySceneBack,this,[=](){
                //设置this出现位置
                this->setGeometry(play_scene->geometry());
                //选关界面隐藏
                play_scene->hide();

                //自身出现
                this->show();
                //重复利用
                delete play_scene;
                play_scene=NULL;

            });


        });



        QLabel *lable = new QLabel;
        lable->setParent(this);
        lable->setFixedSize(levelbtn->width(),levelbtn->height());
        lable->setText(QString::number(i+1));
        lable->move(i%4 *70+25, i/4*70+140);
        //lable文字对齐方式
        lable->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        //鼠标穿透lable
        lable->setAttribute(Qt::WA_TransparentForMouseEvents);


    }








};
void chooseScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/bg2.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //画背景的图标
    pix.load(":/res/Title.png");
    pix=pix.scaled(pix.width(),pix.height());
    painter.drawPixmap(20,20,pix);

}

